import 'dart:io';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/models/xindong_file_select_class.dart';
import 'package:xindong/models/xindong_photo_class.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/container/xindong_gesture_vertical_drag_container.dart';
import 'package:xindong/widgets/image/xindong_image_scale_container.dart';

class XinDongImagesBrowser extends StatefulWidget {
  final List list;
  final int currentIndex;
  final bool isFile;

  const XinDongImagesBrowser({Key? key, required this.list, this.currentIndex = 0, this.isFile = false})
      : super(key: key);

  // 显示弹窗方法 - 生成局部Provider
  XinDongImagesBrowser.show({super.key, required this.list, this.currentIndex = 0, this.isFile = false}) {
    if (list.isEmpty) return;
    showDialog(
      useSafeArea: false, // 不用安全区域
      context: currentContext(),
      barrierColor: Colors.transparent,
      builder: (context) {
        return XinDongImagesBrowser(
          list: list,
          currentIndex: currentIndex,
          isFile: isFile,
        );
      },
    );
  }

  @override
  State<XinDongImagesBrowser> createState() => _XinDongImagesBrowserState();
}

class _XinDongImagesBrowserState extends State<XinDongImagesBrowser> {
  late double _opacity = 1.0;
  late int _currentIndex = widget.currentIndex;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      // 触摸外部、返回键时，是否隐藏Dialog
      onWillPop: () async => true,
      child: Material(
        type: MaterialType.transparency,
        child: Scaffold(
          backgroundColor: Colors.black.withOpacity(_opacity),
          body: Swiper(
            loop: false,
            index: _currentIndex,
            itemCount: widget.list.length,
            itemBuilder: (BuildContext context, int index) {
              dynamic object = widget.list[index % (widget.list.length)];
              return XinDongGestureVerticalDragContainer(
                onTap: () => Navigator.pop(context),
                onDragUpdate: (double dragDistance, double scale) {
                  setState(() {
                    _opacity = scale;
                  });
                },
                onDragDone: (bool isDone) {
                  if (isDone) {
                    Navigator.pop(context);
                  } else {
                    setState(() {
                      _opacity = 1;
                    });
                  }
                },
                child: widget.isFile ? _buildImageFile(object) : _buildItemWidget(object),
              );
            },
            onIndexChanged: (index) {
              _currentIndex = index;
            },
            pagination: const SwiperPagination(
              margin: EdgeInsets.only(bottom: 34),
              builder: DotSwiperPaginationBuilder(
                color: Colors.grey,
                activeColor: DZColors.mainColor,
                size: 6.0,
                space: 4.0,
                activeSize: 7.0,
              ),
            ),
          ),
        ),
      ),
    );
  }

  // item
  Widget _buildItemWidget(var object) {
    String urlString;
    if (object is String) {
      urlString = object;
    } else if (object is XinDongPhotoClass) {
      urlString = object.pic.first;
    } else {
      return const Text('Un support');
    }
    return XinDongImageScaleContainer(
      child: ImageUtil.networkImageUrl(
        urlString,
        fit: BoxFit.contain,
        isZoom: false,
        placeholder: ImageUtil.networkImageUrl(
          urlString,
          fit: BoxFit.contain,
          placeholder: Container(color: Colors.black),
        ),
      ),
    );
  }

  Widget _buildImageFile(var object) {
    File file;
    if (object is File) {
      file = object;
    } else if (object is XinDongSelectFileClass) {
      file = object.file;
    } else {
      return const Text('Un support');
    }
    return XinDongImageScaleContainer(
      child: Image.file(file, fit: BoxFit.contain),
    );
  }
}
